﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

namespace WebAppArtreve.views.shared.components.upload
{
    //DIT WORDT NIET GEBRUIKT, UPLOAD GEBEURDT TELKENS IN DE DE WUCS ZELF

    public partial class FileUpload : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void uploadknopje_Click(object sender, EventArgs e)
        {
            UploadFile();
        }

        private void UploadFile()
        {
            //1. FILE SELECTED

            if (this.FileUploadComponentAjax.HasFile)
            {
                //2. FILENAME
                string fileName = Server.HtmlEncode(this.FileUploadComponentAjax.FileName);

                //3. GET EXTENSION OF FILE
                string fileExtension = System.IO.Path.GetExtension(fileName);

                //4. CHECK EXTENSION
                if (fileExtension == ".jpg" ||
                    fileExtension == ".png")
                {
                    //5. CHECK FILSEIZE
                    int fileSize = this.FileUploadComponentAjax.PostedFile.ContentLength;
                    if (fileSize <= 1024 * 1024)
                    {

                        //6. CREATE TARGET PATH
                        string targetFileName = fileName.Split('.')[0] + getTimeStamp() + fileExtension;
                        targetFileName = ConfigurationManager.AppSettings["DMSPATH"] + targetFileName;
                        targetFileName = Server.MapPath(targetFileName);

                        //7. SAVE THE FILE
                        this.FileUploadComponentAjax.SaveAs(targetFileName);
                        lblResult.Text = "File uploaded";
                    }
                    else
                    {
                        lblResult.Text = "Please choose a smaller file, otherwise fuck off.";
                    }

                }
                else
                {
                    lblResult.Text = "Please choose a jpg or a png file, otherwise...";
                }
            }
            else
            {
                lblResult.Text = "Please select a file, otherwise fuck off.";
            }
        }

        private string getTimeStamp()
        {
            var nowDate = DateTime.Now;
            return nowDate.ToString("yyyyMddHHmmssffff");
        }
    }
}